package com.example.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Random;

import org.testng.annotations.DataProvider;

import com.example.fw.ApplicationManager;
import com.example.fw.ContactObject;


public class ContactDataGenerator {

	static Random rnd = new Random();
	static String tab = "\t";
	protected static ApplicationManager app;
	public static String[] MONTHS = new String[]{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
	public static int[] DaysOfMonths = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

	public int generateRandomNumberBetween(int start, int end) {
		return new Random().nextInt(end - start) + start;
	}
	
	@DataProvider(name = "randomContacts")
	public static Iterator<Object[]> generateRandomContacts(){
		List<ContactObject> contacts = new ContactDataGenerator().generateRandomContactList(5);
		List<Object[]> list = new ArrayList<Object[]>();
		for (ContactObject contact : contacts) {
			list.add(new Object[]{contact});
		}
		return list.iterator();
	}

	@DataProvider(name = "contactsFromFile")
	public static Iterator<Object[]> loadContactsFromFile() throws IOException{
		Properties props = new Properties();
		props.load(new FileReader( "application.properties"));
		app = ApplicationManager.getInstance();
		app.setProperties(props);
		List<Object[]> list = new ArrayList<Object[]>();
		BufferedReader reader = new BufferedReader (new FileReader(app.getProperty("contactsDat")));
		String line =  reader.readLine();
		while (line != null) {
			String[] parts = line.split(tab);
			ContactObject contact = new ContactObject()
			.setFirstName(parts[0])
			.setLastName(parts[1])
			.setAddress(parts[2])
			.setHome(parts[3])
			.setMobile(parts[4])
			.setWork(parts[5])
			.setEmail(parts[6])
			.setEmail2(parts[7])
			.setBday(parts[8])
			.setBmonth(parts[9])
			.setByear(parts[10])
			.setSelectgroup(parts[11])
			.setAddress2(parts[12])
			.setPhone2(parts[13]);
			list.add(new Object[]{contact});
			line =  reader.readLine();
		}
		return list.iterator();
	}

	public static void main(String[] args){
		if (args.length < 2){
			System.out.println("Specify two parameters: file and count");
			return;
		}
		String file = args[0];
		int count = Integer.parseInt(args[1]);
		try {
			new ContactDataGenerator().generateDataToFile(file, count);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void generateDataToFile(String fileName, int count) throws IOException {
		List<ContactObject> contacts = generateRandomContactList(count);
		File file = new File(fileName);
		if (file.exists()) {
			file.delete();
		}
		FileWriter writer = new FileWriter(file);
		for (ContactObject contact : contacts) {
			writer.write(contact.firstName + tab + contact.lastName + tab + contact.address + tab + contact.home + tab + contact.mobile + tab +
					contact.work + tab + contact.email + tab + contact.email2 + tab + contact.bday + tab + contact.bmonth + tab + contact.byear + tab +
					contact.selectgroup + tab + contact.address2 + tab + contact.phone2 + "\r\n");
		}
		writer.close();
		
	}

	private List<ContactObject> generateRandomContactList(int count) {
		List<ContactObject> list = new ArrayList<ContactObject>();
		for (int i = 0; i < count; i++) {
			int month = rnd.nextInt(12);
			ContactObject contact = new ContactObject()
				.setFirstName("FN" + rnd.nextInt())
				.setLastName("LN" + rnd.nextInt())
				.setAddress("A" + rnd.nextInt())
				.setHome("H" + rnd.nextInt())
				.setMobile("M" + rnd.nextInt())
				.setWork("W" + rnd.nextInt())
				.setEmail("E" + rnd.nextInt())
				.setEmail2("E2" + rnd.nextInt())
				.setBday("" + generateRandomNumberBetween(1,DaysOfMonths[month]))
				.setBmonth(MONTHS[month])
				.setByear("" + generateRandomNumberBetween(1900,2012))
				.setSelectgroup("ruit")
				.setAddress2("A2" + rnd.nextInt())
				.setPhone2("P2" + rnd.nextInt());
			list.add(contact);
		}
		return list;
	}
}
